<?php
// +----------------------------------------------------------------------
// | KITEGO-Admin「开箱即用」「人人全栈」
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.kitego.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed KITEGO并不是自由软件，未经许可不能去掉KITEGO相关版权
// +----------------------------------------------------------------------
// | Author: KITEGO Team <bd@kitego.cn>
// +----------------------------------------------------------------------

namespace kitego\extend\storage;

use kitego\enums\AttachmentEnum;

abstract class Storage implements StorageInterface
{
    protected $config;
    protected $file;
    protected $error;
    protected $fileName;
    protected $fileInfo;

    /**
     * 设置上传的文件信息
     */
    public function setUploadFile($name, $uploadType)
    {
        // 接收上传的文件
        $this->file = request()->file($name);

        if (empty($this->file)) exception('未找到上传文件的信息');

        // 校验上传文件后缀
        if (!in_array(strtolower($this->file->extension()), AttachmentEnum::getFileUploadExt($uploadType))) {
            exception($uploadType . '不允许上传' . $this->file->extension() . '文件');
        }

        // 处理文件名称
        $fileOriginName = $this->file->getOriginalName();
        if (strlen($fileOriginName) > 128) {
            $name = substr($fileOriginName, 0, 123);
            $nameEnd = substr($fileOriginName, strlen($fileOriginName) - 5, strlen($fileOriginName));
            $fileNameHandle = $name . $nameEnd;
        }

        // 文件信息
        $this->fileInfo = [
            'ext' => $this->file->extension(),
            'size' => $this->file->getSize(),
            'mime' => $this->file->getMime(),
            'name' => $fileNameHandle ?? $this->file->getOriginalName(), // 原始文件名称
            'realPath' => $this->file->getRealPath(),
            'storage_id' => $this->config['id'],
            'storage_type' => $this->config['type'],
            'domain' => !isset($this->config['domain']) ? '' : $this->config['domain']
        ];

        // 实际路径文件名
        $this->fileName = $this->buildFileSaveName();
    }

    /**
     * 生成保存文件名
     */
    private function buildFileSaveName()
    {
        // 要上传图片的本地路径
        $realPath = $this->getRealPath();

        // 扩展名
        $ext = pathinfo($this->getFileInfo()['name'], PATHINFO_EXTENSION);

        // 自动生成文件名
        return date('YmdHis') . substr(md5($realPath), 0, 5) . str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT) . ".{$ext}";
    }

    protected function getRealPath()
    {
        return $this->fileInfo['realPath'];
    }

    public function getFileInfo()
    {
        return $this->fileInfo;
    }

    public function getFileName()
    {
        return $this->fileName;
    }

    public function setUploadDir($saveDir = 'uploads/attachment')
    {
        return $saveDir . '/' . date('Ymd');
    }

    public function getError()
    {
        return $this->error;
    }
}